package com.loren.tetris.windows.ui;

import com.loren.tetris.data.GameData;
import com.loren.tetris.data.constant.DataConstant;
import com.loren.tetris.utils.AssistiveUtils;

import java.awt.*;
import java.math.BigDecimal;

public class BoxCount extends Box {

    public BoxCount(int boxX, int boxY, int boxW, int boxH, GameData gameData) {
        super(boxX, boxY, boxW, boxH, gameData);
    }

    @Override
    protected void gameAction(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        int x = this.getBoxX() + 10, y = this.getBoxY() + 30, w = this.getBoxW() - 20;

        g2d.setColor(DataConstant.COLOR_WHITE);
        g2d.setFont(new Font(AssistiveUtils.getFontFamily(), Font.BOLD, 18));
        g2d.drawString("分数", x, y);
        g2d.drawString("消行", x, y + 40);

        g2d.setFont(new Font(AssistiveUtils.getFontFamily(), Font.PLAIN, 18));
        g2d.setColor(DataConstant.COLOR_RED);
        g2d.drawString(String.valueOf(this.getGameData().getGameScore()), x + 50, y + 2);
        g2d.drawString(String.valueOf(this.getGameData().getGameRemoveCount()), x + 50, y + 42);

        g2d.setColor(DataConstant.COLOR_GAME_TIME);
        g2d.setFont(new Font(AssistiveUtils.getFontFamily(), Font.PLAIN, 32));
        String hour = this.formatGameTime(this.getGameData().getGameTimeHour());
        String minute = this.formatGameTime(this.getGameData().getGameTimeMinute());
        String second = this.formatGameTime(this.getGameData().getGameTimeSecond());
        g2d.drawString(hour + ":" + minute + ":" + second, x + 140, y + 30);

        g2d.setFont(new Font(AssistiveUtils.getFontFamily(), Font.PLAIN, 16));
        g2d.setColor(DataConstant.COLOR_WHITE);
        g2d.drawString("下一级", x + 10, y + 90);

        g2d.drawRect(x, y + 70, w, 30);
        g2d.setColor(DataConstant.PROGRESS_BAR_COLOR);

        int currentGradeScore = AssistiveUtils.getNextGameGradeScore(this.getGameData().getGameGrade() - 1);
        int nextGradeScore = AssistiveUtils.getNextGameGradeScore(this.getGameData().getGameGrade());

        BigDecimal proportion = BigDecimal.valueOf((this.getGameData().getGameScore() - currentGradeScore) / ((nextGradeScore - currentGradeScore) * 1.0));
        double width = proportion.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() * w;
        g2d.fillRect(x, y + 70, (int) width, 30);
    }

    private String formatGameTime(int gameTime) {
        return gameTime < 10 ? "0" + gameTime : String.valueOf(gameTime);
    }


}
